'use strict'

angular.module("mainApp")
    .constant('ngPaginationConfig', {
        visiblePageCount: 10,
        pageSize: 5,
        showIfOnePage: false,
        showFirstLastText: true,
        showGoto: true
    })
    .directive("pager", ['ngPaginationConfig','$filter', function (ngPaginationConfig,$filter) {
    return {
        link: function (scope, element, attrs) {
            var visiblePageCount = angular.isDefined(attrs.visiblePageCount) ? attrs.visiblePageCount : ngPaginationConfig.visiblePageCount;
            var pageSize = angular.isDefined(attrs.pageSize) ? attrs.visiblePageCount : ngPaginationConfig.pageSize;
            scope.showFirstLastText = angular.isDefined(attrs.showFirstLastText) ? attrs.showFirstLastText : ngPaginationConfig.showFirstLastText;
            scope.showIfOnePage = angular.isDefined(attrs.showIfOnePage) ? attrs.showIfOnePage : ngPaginationConfig.showIfOnePage;
            scope.showGoto = angular.isDefined(attrs.showGoto) ? attrs.showGoto : ngPaginationConfig.showGoto;
            scope.currentPage = 1;
            var list = scope.pageList;
            scope.pageChange = function (page) {
                if (page >= 1 && page <= scope.pageCount) {
                    scope.currentPage = page;
                } else {
                    scope.currentPage = 1;
                }
            }
            scope.keyupHanlder = function (e) {
                var value = e.target.value;
                var parsedValue = parseInt(value, 10);
                if (!Number.isNaN(parsedValue)) {
                    if (parsedValue >= 1 && parsedValue <= scope.pageCount) {

                    } else if (parsedValue < 1) {
                        e.target.value = 1;
                    } else {
                        e.target.value = scope.pageCount;
                    }
                    if (e.keyCode === 13) {
                        // pressed enter
                        scope.currentPage = parsedValue;
                    }
                } else {
                    if (e.preventDefault) {
                        e.preventDefault();
                    } else {
                        return false;
                    }
                }
            }

            function build() {
                var low,
                    high,
                    v;

                scope.pagenums = [];
                if (scope.pageCount === 0) {
                    scope.pageList=[];
                    return;
                }
                var i=0,j;
                if (scope.currentPage > scope.pageCount) {
                    scope.currentPage = 1;
                }else{
                    i = (scope.currentPage-1)*pageSize;
                }
                j =i+pageSize>list.length?list.length:i+pageSize;
                var items=[];
                for(;i<j;i++){
                    items.push(list[i]);
                }
                scope.pageList=items;
                if (scope.pageCount <= visiblePageCount) {
                    low = 1;
                    high = scope.pageCount;
                } else {
                    v = Math.ceil(visiblePageCount / 2);
                    low = Math.max(scope.currentPage - v, 1);
                    high = Math.min(low + visiblePageCount - 1, scope.pageCount);

                    if (scope.pageCount - high < v) {
                        low = high - visiblePageCount + 1;
                    }
                }

                for (; low <= high; low++) {
                    scope.pagenums.push(low);
                }
            }

            scope.$watch('currentPage', function (a, b) {
                if (a !== b) {
                    build();
                    scope.onPageChange();
                }
            });

            scope.$watch('pageCount', function (a, b) {
                if (a>=0) {
                    build();
                }
            });
            scope.$watch('newPageList', function (a, b) {

                if (a) {
                    list = scope.newPageList;
                    var count = list.length%pageSize==0?list.length/pageSize:parseInt(list.length/pageSize)+1;
                    scope.currentPage = 1;
                    if(scope.pageCount==count){
                        build();
                    }else{
                        scope.pageCount=count;
                    }
                }
            });

        },
        replace: true,
        restrict: "E",
        scope: {
            pageList: '=',
            newPageList: '=',
            currentPage: '=',
            onPageChange: '&',
        },
        templateUrl: '/template/pagination.html'
    }
}])